package com.mysoft.gfyx.common.util;

import com.mysoft.framework.context.api.ContextManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

@Slf4j
@Component
public class LogUtil {

    @Autowired
    private ContextManager contextManager;

    public void error(String category, Exception e) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        log.error("tenant_code:" + contextManager.fetchContextInfo().getTenantCode()
                + ";user_code:" + contextManager.fetchContextInfo().getUserContext().getUserCode()
                + ";category:" + category
                + ";msg:" + e.getMessage()
                + ";trace:" + byteArrayOutputStream.toString()
        );

    }

    public void info(String category, String infoMsg) {
        log.info("tenant_code:" + contextManager.fetchContextInfo().getTenantCode()
                + ";user_code:" + contextManager.fetchContextInfo().getUserContext().getUserCode()
                + ";category:" + category
                + ";info:" + infoMsg);
    }
}
